package com.llsydn.a03dropdown;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;

import java.util.ArrayList;

/**
 * @author lilinshen
 * @title 下拉列表
 * @description 请填写相关描述
 * @date 2018/10/15 15:28
 */
public class MainActivity extends AppCompatActivity {

    private EditText et_input;
    private ImageView iv_down_arrow;
    private PopupWindow popupWindow;
    private ListView listView;
    private ArrayList<String> msgs;
    private MyAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化值
        et_input = findViewById(R.id.et_input);
        iv_down_arrow = findViewById(R.id.iv_down_arrow);

        //设置点击事件
        et_input.setOnClickListener((v) -> {
            if (popupWindow == null) {
                popupWindow = new PopupWindow(MainActivity.this);
                popupWindow.setWidth(et_input.getWidth());
                popupWindow.setHeight(DensityUtil.dip2px(MainActivity.this, 200));
                popupWindow.setContentView(listView);
                popupWindow.setFocusable(true);
            }
            popupWindow.showAsDropDown(et_input, 0, 0);
        });
        listView = new ListView(this);
        listView.setBackgroundResource(R.drawable.listview_background);
        //准备数据
        msgs = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            msgs.add(i + "--aaaaaa--" + i);
        }
        adapter = new MyAdapter();
        listView.setAdapter(adapter);
        listView.setOnItemClickListener((parent, view, position, id) -> {
            String msg = msgs.get(position);
            et_input.setText(msg);
            if (popupWindow != null && popupWindow.isShowing()) {
                popupWindow.dismiss();
                popupWindow = null;
            }
        });
    }

    /**
     * 自定义adapter类
     */
    class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return msgs.size();
        }

        @Override
        public Object getItem(int position) {
            return msgs.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;
            if (convertView == null) {
                convertView = View.inflate(MainActivity.this, R.layout.item_main, null);
                viewHolder = new ViewHolder();
                viewHolder.tv_msg = convertView.findViewById(R.id.tv_msg);
                viewHolder.iv_delete = convertView.findViewById(R.id.iv_delete);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            String msg = msgs.get(position);
            viewHolder.tv_msg.setText(msg);
            //设置删除
            viewHolder.iv_delete.setOnClickListener((v) -> {
                msgs.remove(position);
                adapter.notifyDataSetChanged();
            });
            return convertView;
        }
    }

    static class ViewHolder {
        TextView tv_msg;
        ImageView iv_delete;
    }

}
